﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Teaching;
using System.Drawing;

namespace School.Pages.Teaching
{
    public partial class OfflineExams : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1510) == true)
                {
                    if (!this.Page.IsPostBack)
                        FillControls();
                    lbltst.Visible = false;
                    lblgrid.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        private void FillControls()
        {
            ddlCampus.DataSource = School.Uniform.PayOrder.AllCampus();
            ddlCampus.DataBind();
        }

        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }

        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.DataSource = School.Uniform.PayOrder.GetStadgeByCertificate(int.Parse(ddlCertificate.SelectedValue));
                ddlBind(ddlStadge);
            }

        }
        protected void ddlStadge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStadge.SelectedValue != "0")
            {
                ddlGrade.DataSource = School.Uniform.PayOrder.GetGradeByStadge(int.Parse(ddlStadge.SelectedValue));
                ddlBind(ddlGrade);
            }

        }
        protected void ddlCampus_TextChanged(object sender, EventArgs e)
        {
            if (ddlCampus.SelectedValue != "0")
            {

                ddlCertificate.DataSource = School.Uniform.PayOrder.GetCertificateByCamp(int.Parse(ddlCampus.SelectedValue));
                ddlBind(ddlCertificate);
            }
        }

        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }

        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGrade.SelectedIndex > 0)
            {
                //Get_Exam
                ddlExam.DataSource = OfflineExam.Get_Exam(int.Parse(ddlGrade.SelectedValue));
                ddlBind(ddlExam);
            }
        }

        private void gvinExamBind()
        {
            gvinExam.DataSource = OfflineExam.getSubjectsByGradein(long.Parse(ddlGrade.SelectedValue), int.Parse(ddlExam.SelectedValue));
            gvinExam.DataBind();
        }

        private void gvExamBind()
        {
            gvExam.DataSource = OfflineExam.getSubjectsByGrade(long.Parse(ddlGrade.SelectedValue), int.Parse(ddlExam.SelectedValue));
            gvExam.DataBind();
        }
        protected void btnsaveExam_Click(object sender, EventArgs e)
        {
            bool tst = false;

            foreach (GridViewRow row in gvExam.Rows)
            {
                CheckBox ch = (CheckBox)gvExam.Rows[row.RowIndex].FindControl("ch");
                if (ch.Checked)
                {

                    int sub = Convert.ToInt32(gvExam.DataKeys[row.RowIndex].Value.ToString());
                    TextBox Min = (TextBox)gvExam.Rows[row.RowIndex].FindControl("TxtMin");
                    TextBox max = (TextBox)gvExam.Rows[row.RowIndex].FindControl("TxtMax");
                    TextBox txtDate = (TextBox)gvExam.Rows[row.RowIndex].FindControl("txtDate");
                    TextBox txtDuration = (TextBox)gvExam.Rows[row.RowIndex].FindControl("txtDuration");

                    if (string.IsNullOrEmpty(Min.Text))
                        Min.Text = "0";
                    if (string.IsNullOrEmpty(max.Text))
                        max.Text = "0";
                    tst = OfflineExam.Add_OfflineExam_Sub(Convert.ToInt32(ddlExam.SelectedValue), sub, decimal.Parse(max.Text), decimal.Parse(Min.Text), Convert.ToDecimal(txtDuration.Text), Convert.ToDateTime(txtDate.Text));
                }
            }

            if (tst)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تمت الاضافة بنجاح";
                gvExamBind();
                gvinExamBind();
                if (gvinExam.Rows.Count > 0)
                    fgin.Visible = true;
                else
                    fgin.Visible = false;
            }

            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltst.Visible = true;

        }

        protected void gvinExam_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvinExam.EditIndex = e.NewEditIndex;
            gvinExamBind();
        }

        protected void gvinExam_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvinExam.EditIndex = -1;
            gvinExamBind();
        }

        protected void gvinExam_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool re = false;
            int id = Convert.ToInt32(gvinExam.DataKeys[e.RowIndex].Value.ToString());
            re = OfflineExam.Delete_Offline_Exam_Subject(id);
            if (re == true)
            {
                lblgrid.ForeColor = Color.Green;
                lblgrid.Text = "تم الحذف بنجاح";
                gvinExamBind();
                gvExamBind();
                if (gvinExam.Rows.Count > 0)
                    fgin.Visible = true;
                else
                    fgin.Visible = false;
            }

            else
            {
                lblgrid.ForeColor = Color.Red;
                lblgrid.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lblgrid.Visible = true;
        }

        protected void gvinExam_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = Convert.ToInt32(gvinExam.DataKeys[e.RowIndex].Value.ToString());
            TextBox txtmax = (TextBox)gvinExam.Rows[e.RowIndex].FindControl("txtmax");
            TextBox txtmin = (TextBox)gvinExam.Rows[e.RowIndex].FindControl("txtmin");
            TextBox txtDate = (TextBox)gvinExam.Rows[e.RowIndex].FindControl("txtDate");
            TextBox txtDuration = (TextBox)gvinExam.Rows[e.RowIndex].FindControl("txtDuration");
            bool re = OfflineExam.Updat_Offline_Exam_Subject(id, Convert.ToDecimal(txtmax.Text), Convert.ToDecimal(txtmin.Text), Convert.ToDecimal(txtDuration.Text), Convert.ToDateTime(txtDate.Text));
            if (re == true)
            {
                lblgrid.ForeColor = Color.Green;
                lblgrid.Text = "تم التحديث بنجاح";
                gvinExam.EditIndex = -1;
                gvinExamBind();
            }
            else
            {
                lblgrid.ForeColor = Color.Red;
                lblgrid.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lblgrid.Visible = true;
        }

        protected void ddlExam_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlExam.SelectedIndex > 0)
            {
                gvExamBind();
                //getSubjectsByGradein
                gvinExamBind();

                if (gvExam.Rows.Count > 0)
                {
                    Panel1.Visible = true;
                }
                else
                {
                    Panel1.Visible = false;
                }
                if (gvinExam.Rows.Count > 0)
                    fgin.Visible = true;
                else
                    fgin.Visible = false;
            }
        }
    }
}